package com.gift.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gift.auto.JSONAuthentication;
import com.gift.entity.GiUser;
import com.gift.entity.LpcType;
import com.gift.entity.UserMeans;
import com.gift.mapper.LpcTypeMapper;
import com.gift.response.Result;
import com.gift.utils.UpPhotoNameUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gift
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/lpc-type")
public class LpcTypeController extends JSONAuthentication {
    @Autowired
    LpcTypeMapper lpcTypeMapper;
    @ApiOperation(value = "添加礼品册的类型")
    @PostMapping("/add")
    public void addCar(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response, @RequestParam("price") int price,@RequestParam("name") String name,@RequestParam("content") String content) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("price",price);
        LpcType baMeans1=lpcTypeMapper.selectOne(wrapper);
        if(baMeans1!=null){
            Result error = Result.error().message("该价格礼品册已存在 不可添加");
            this.WriteJSON(request, response, error);
        }else{
            LpcType lpcType=new LpcType();
            lpcType.setPrice(price);
            lpcType.setName(name);
            lpcType.setContent(content);
            byte[] bytes = file.getBytes();
            String imageFileName = file.getOriginalFilename();
            String fileName = UpPhotoNameUtils.getPhotoName("img",imageFileName);
            Path path = Paths.get("D:\\自己的项目\\礼品册销售系统\\gif\\src\\assets\\picture\\" + fileName);
            //“C:\\框架\\D4\\d4_pc_ui\\src\\assets\\images\\img\\”为本地目录
            Files.write(path, bytes);//写入文件
            String avatar_url=fileName;
            lpcType.setPricture(avatar_url);
            response.reset();
            if(lpcTypeMapper.insert(lpcType)>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }
    }
    @ApiOperation(value = "删除礼品册类型")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=lpcTypeMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有礼品册类型")
    @PostMapping("/findAllLpc")
    public void findAllLpc(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<LpcType> shUserPage=new Page<>(current,size);
        Page<LpcType> shUserPages=lpcTypeMapper.selectPage(shUserPage,null);
        List<LpcType> records = shUserPages.getRecords();
        int total=lpcTypeMapper.selectCount(null);
        if(records.size()>0){
            Result ok = Result.ok().data("LpcType",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
//    @ApiOperation(value = "根据礼品册类型查找可兑换的商品")
//    @PostMapping("/findAllLpcByType")
//    public void findAllLpcByType(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
//        Page<LpcType> shUserPage=new Page<>(current,size);
//        Page<LpcType> shUserPages=lpcTypeMapper.selectPage(shUserPage,null);
//        List<LpcType> records = shUserPages.getRecords();
//        int total=lpcTypeMapper.selectCount(null);
//        if(records.size()>0){
//            Result ok = Result.ok().data("LpcType",records).data("total",total);
//            this.WriteJSON(request, response, ok);
//        } else {
//            Result error = Result.error();
//            this.WriteJSON(request, response, error);
//        }
//    }
@ApiOperation(value = "查找礼品册类型通过礼品册类型id")
@PostMapping("/findAllLpcById")
public void findAllLpcById(HttpServletRequest request, HttpServletResponse response,@RequestParam("id") int id)throws  Exception{
    LpcType lpcType = lpcTypeMapper.selectById(id);
    if(lpcType!=null){
        Result ok = Result.ok().data("LpcType",lpcType);
        this.WriteJSON(request, response, ok);
    } else {
        Result error = Result.error();
        this.WriteJSON(request, response, error);
    }
}
}

